package com.photoeditor.demo.ui.widget.image;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;

import com.photoeditor.R;
import com.photoeditor.demo.model.image.gl.IPosition;
import com.photoeditor.demo.util.FetcherHolder;
import com.photoeditor.demo.util.IGetCacheListener;
import com.photoeditor.demo.util.ILoadDiskCache;
import com.photoeditor.demo.util.ImageCache;

/**
 * 视频预览图
 */
public class VideoImageView extends RelativeLayout implements ILoadDiskCache, IPosition {
    private ImageView mPlayView;
    private ImageView mVideoView;
    private ImageView mDisplayView;
    private RelativeLayout mDisplayLayout;
    private int mPosition;
    private String mPath;
    public IGetCacheListener mListener;

    public VideoImageView(Context context) {
        super(context);
        initView();
    }

    public VideoImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public VideoImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.layout_video_image, this, true);
        mVideoView = (ImageView) findViewById(R.id.video_image);
        mPlayView = (ImageView) findViewById(R.id.play_video);
        mDisplayView = (ImageView) findViewById(R.id.display_view);
        mDisplayLayout = (RelativeLayout) findViewById(R.id.display_layout);
    }

    public ImageView getVideoImageView() {
        return mVideoView;
    }

    public void setVideoImageViewListener(OnClickListener mListener) {
        if (mVideoView != null) {
            mVideoView.setOnClickListener(mListener);
        }
    }

    public void setPlayVideoListener(OnClickListener mListener) {
        if (mPlayView != null) {
            mPlayView.setOnClickListener(mListener);
        }
    }

    public ImageView getDisplayView() {
        return mDisplayView;
    }

    public void setDisplayViewGone() {
        mDisplayView.setVisibility(View.GONE);
    }

    public void setPlayViewGone() {
        mPlayView.setVisibility(View.GONE);
    }

    public RelativeLayout getDisplayLayout() {
        return mDisplayLayout;
    }

    public void setDisplayLayoutPaddingBottom(int resId) {
        mDisplayLayout.setPadding(0, 0, 0, getContext().getResources().getDimensionPixelSize(resId));
    }

    @Override
    public int getPosition() {
        return mPosition;
    }

    @Override
    public void setPosition(int position) {
        mPosition = position;
    }

    @Override
    public void loadDiskCache(ImageCache cache, String path, IGetCacheListener listener) {
        mListener = listener;
        mPath = path;
        FetcherHolder.getSmallImageFetcher().loadPreViewDiskCache(path, this);
    }

    @Override
    public void setCacheMiniBitmap(Bitmap bitmap) {
        ImageView v = getDisplayView();
        if (bitmap != null && !bitmap.isRecycled() && v.getVisibility() == View.VISIBLE) {
            v.setScaleType(ScaleType.FIT_CENTER);
            v.setImageBitmap(bitmap);
        } else if ((bitmap == null || bitmap.isRecycled()) && v.getVisibility() == View.VISIBLE) {
            if (mListener != null) {
                mListener.onLoadError();
            }
        }
    }

    @Override
    public String getPath() {
        return mPath;
    }
}
